package com.qyhl.module_practice.score.mine.bill;

import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.module_practice.R;
import com.qyhl.module_practice.R2;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.DateUtils;
import com.qyhl.webtv.basiclib.utils.NetUtil;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeScoreBillBean;
import com.qyhl.webtv.commonlib.utils.action.ActionConstant;
import com.qyhl.webtv.commonlib.utils.action.ActionLogUtils;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Objects;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterPathConstant.PRACTICE_SCORE_BILL)
public class PracticeScoreBillActivity extends BaseActivity implements PracticeScoreBillContract.PracticeScoreBillView {
    @BindView(R2.id.recycle_view)
    RecyclerView recycleView;
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;
    @BindView(R2.id.year)
    TextView year;
    @BindView(R2.id.month)
    TextView month;
    @BindView(R2.id.recycle_date)
    RecyclerView recycleDate;
    @BindView(R2.id.date_layout)
    LinearLayout dateLayout;

    private LinearLayoutManager linearLayoutManager;

    private String volId;

    private CommonAdapter commonAdapter;

    private CommonAdapter dateAdapter;

    private List<PracticeScoreBillBean> mDataList = new ArrayList<>();

    private List<PracticeBillDateBean> dateList = new ArrayList<>();

    private List<PracticeBillDateBean> yearList = new ArrayList<>();

    private List<PracticeBillDateBean> monthList = new ArrayList<>();

    private PracticeScoreBillPresenter mPresenter;

    private String sYear, sMonth;

    private Drawable[] yeardrawables, monthdrawables;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected int getContentView() {
        return R.layout.practice_activity_score_bill;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        setDefaultImmerBar();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        volId = getIntent().getStringExtra("id");
        mPresenter = new PracticeScoreBillPresenter(this);
        initView();
    }

    @Override
    protected void setListener() {
        loadMask.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                mPresenter.getList(volId, sYear, sMonth);
            }
        });

        dateAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                if (dateList.get(position).isYear()) {
                    for (int i = 0; i < yearList.size(); i++) {
                        if (position == i) {
                            yearList.get(i).setIschecked(true);
                            dateList.get(i).setIschecked(true);
                        } else {
                            yearList.get(i).setIschecked(false);
                            dateList.get(i).setIschecked(false);
                        }
                    }
                    sYear = dateList.get(position).getDate().replace("年", "");
                    year.setText(dateList.get(position).getDate());
                    yeardrawables = year.getCompoundDrawables();
                    if (Objects.requireNonNull(yeardrawables[2].getConstantState()).equals(getResources().getDrawable(R.drawable.practice_arrow_up_icon).getConstantState())) {
                        year.setCompoundDrawablesWithIntrinsicBounds(null, null, getResources().getDrawable(R.drawable.practice_arrow_down_icon), null);
                    }
                } else {
                    for (int i = 0; i < monthList.size(); i++) {
                        if (position == i) {
                            monthList.get(i).setIschecked(true);
                            dateList.get(i).setIschecked(true);
                        } else {
                            monthList.get(i).setIschecked(false);
                            dateList.get(i).setIschecked(false);
                        }
                    }
                    monthdrawables = month.getCompoundDrawables();
                    if (Objects.requireNonNull(monthdrawables[2].getConstantState()).equals(getResources().getDrawable(R.drawable.practice_arrow_up_icon).getConstantState())) {
                        month.setCompoundDrawablesWithIntrinsicBounds(null, null, getResources().getDrawable(R.drawable.practice_arrow_down_icon), null);
                    }
                    sMonth = dateList.get(position).getDate().replace("月", "");
                    month.setText(dateList.get(position).getDate());
                }
                dateAdapter.notifyDataSetChanged();

                dateLayout.setVisibility(View.GONE);
                loadMask.setStatus(LoadingLayout.Loading);
                mPresenter.getList(volId, sYear, sMonth);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
    }

    @SuppressLint("SetTextI18n")
    private void initView() {
        loadMask.setStatus(LoadingLayout.Success);
        refresh.setRefreshHeader(new MaterialHeader(this));
        refresh.setRefreshFooter(new ClassicsFooter(this));
        refresh.setEnableLoadMore(true);

        recycleDate.setLayoutManager(new LinearLayoutManager(this));
        recycleDate.setAdapter(dateAdapter = new CommonAdapter<PracticeBillDateBean>(this, R.layout.practice_item_bill_date, dateList) {
            @Override
            protected void convert(ViewHolder holder, PracticeBillDateBean bean, int position) {
                holder.setText(R.id.date, bean.getDate());
                if (bean.isIschecked()) {
                    holder.setTextColorRes(R.id.date, R.color.global_base);
                    holder.setVisible(R.id.tag, true);
                } else {
                    holder.setTextColorRes(R.id.date, R.color.global_black_lv1);
                    holder.setVisible(R.id.tag, false);
                }
            }
        });

        linearLayoutManager = new LinearLayoutManager(this);
        recycleView.setLayoutManager(linearLayoutManager);
        recycleView.setAdapter(commonAdapter = new CommonAdapter<PracticeScoreBillBean>(this, R.layout.practice_item_score_bill, mDataList) {
            @Override
            protected void convert(ViewHolder holder, PracticeScoreBillBean bean, int position) {
                holder.setText(R.id.date, DateUtils.practiceSignFormat(bean.getTime()));
                switch (bean.getType()) {
                    case "transfer":
                        holder.setText(R.id.content, bean.getTransfer().getSender() + "赠送积分给" + bean.getTransfer().getReceiver());
                        if (bean.getTransfer().getReciverVolid().equals(volId)) {
                            holder.setText(R.id.score, "+" + bean.getTransfer().getScore() + "分");
                        } else {
                            holder.setText(R.id.score, "-" + bean.getTransfer().getScore() + "分");
                        }
                        break;
                    case "exchange":
                        holder.setText(R.id.content, bean.getExchange().getDetail());
                        holder.setText(R.id.score, "-" + bean.getExchange().getScore() + "分");
                        break;
                    case "check":
                        holder.setText(R.id.content, bean.getCheck().getDetail());
                        holder.setText(R.id.score, "+" + bean.getCheck().getScore() + "分");
                        break;
                    default:
                        break;
                }
            }
        });

        Calendar c = Calendar.getInstance();

        int curYear = c.get(Calendar.YEAR);
        int curMonth = c.get(Calendar.MONTH) + 1;

        year.setText(curYear + "年");
        month.setText(curMonth + "月");

        sYear = String.valueOf(curYear);
        sMonth = String.valueOf(curMonth);

        for (int i = 0; i < 11; i++) {
            if (i == 0) {
                yearList.add(new PracticeBillDateBean((curYear - i) + "年", true, true));
            } else {
                yearList.add(new PracticeBillDateBean((curYear - i) + "年", false, true));
            }
        }

        for (int i = 0; i < 12; i++) {
            if (i == curMonth - 1) {
                monthList.add(new PracticeBillDateBean((i + 1) + "月", true, false));
            } else {
                monthList.add(new PracticeBillDateBean((i + 1) + "月", false, false));
            }
        }

        mPresenter.getList(volId, sYear, sMonth);
    }


    @Override
    public void setList(List<PracticeScoreBillBean> beanList) {
        loadMask.setStatus(LoadingLayout.Success);
        loadMask.setReloadButtonText("点击重试~");

        mDataList.clear();
        mDataList.addAll(beanList);
        commonAdapter.notifyDataSetChanged();
    }

    @Override
    public void setError(String msg) {
        loadMask.setStatus(LoadingLayout.Error);
        loadMask.setReloadButtonText("点击重试~");

        if (!NetUtil.isNetworkAvalible(this)) {
            loadMask.setErrorImage(R.drawable.error_network);
            loadMask.setErrorText("网络异常，请检查您的网络！");
        } else if (msg.contains("暂无")) {
            loadMask.setErrorImage(R.drawable.empty_content);
            loadMask.setErrorText(msg);
        } else {
            loadMask.setErrorImage(R.drawable.error_content);
            loadMask.setErrorText(msg);
        }
    }


    @OnClick({R2.id.back_btn, R2.id.year, R2.id.month, R2.id.outside})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.back_btn) {
            finish();
        } else if (id == R.id.year) {
            yeardrawables = year.getCompoundDrawables();
            monthdrawables = month.getCompoundDrawables();
            if (Objects.requireNonNull(monthdrawables[2].getConstantState()).equals(getResources().getDrawable(R.drawable.practice_arrow_up_icon).getConstantState())) {
                month.setCompoundDrawablesWithIntrinsicBounds(null, null, getResources().getDrawable(R.drawable.practice_arrow_down_icon), null);
            }

            if (Objects.requireNonNull(yeardrawables[2].getConstantState()).equals(getResources().getDrawable(R.drawable.practice_arrow_down_icon).getConstantState())) {
                year.setCompoundDrawablesWithIntrinsicBounds(null, null, getResources().getDrawable(R.drawable.practice_arrow_up_icon), null);
                dateList.clear();
                dateList.addAll(yearList);
                dateAdapter.notifyDataSetChanged();
                dateLayout.setVisibility(View.VISIBLE);
            } else {
                year.setCompoundDrawablesWithIntrinsicBounds(null, null, getResources().getDrawable(R.drawable.practice_arrow_down_icon), null);
                dateLayout.setVisibility(View.VISIBLE);
            }
        } else if (id == R.id.month) {
            yeardrawables = year.getCompoundDrawables();
            monthdrawables = month.getCompoundDrawables();
            if (Objects.requireNonNull(yeardrawables[2].getConstantState()).equals(getResources().getDrawable(R.drawable.practice_arrow_up_icon).getConstantState())) {
                year.setCompoundDrawablesWithIntrinsicBounds(null, null, getResources().getDrawable(R.drawable.practice_arrow_down_icon), null);
            }

            if (Objects.requireNonNull(monthdrawables[2].getConstantState()).equals(getResources().getDrawable(R.drawable.practice_arrow_down_icon).getConstantState())) {
                month.setCompoundDrawablesWithIntrinsicBounds(null, null, getResources().getDrawable(R.drawable.practice_arrow_up_icon), null);
                dateList.clear();
                dateList.addAll(monthList);
                dateAdapter.notifyDataSetChanged();
                dateLayout.setVisibility(View.VISIBLE);
            } else {
                month.setCompoundDrawablesWithIntrinsicBounds(null, null, getResources().getDrawable(R.drawable.practice_arrow_down_icon), null);
                dateLayout.setVisibility(View.VISIBLE);
            }
        } else if (id == R.id.outside) {
            yeardrawables = year.getCompoundDrawables();
            monthdrawables = month.getCompoundDrawables();
            if (Objects.requireNonNull(monthdrawables[2].getConstantState()).equals(getResources().getDrawable(R.drawable.practice_arrow_up_icon).getConstantState())) {
                month.setCompoundDrawablesWithIntrinsicBounds(null, null, getResources().getDrawable(R.drawable.practice_arrow_down_icon), null);
            }

            if (Objects.requireNonNull(yeardrawables[2].getConstantState()).equals(getResources().getDrawable(R.drawable.practice_arrow_up_icon).getConstantState())) {
                year.setCompoundDrawablesWithIntrinsicBounds(null, null, getResources().getDrawable(R.drawable.practice_arrow_down_icon), null);
            }

            dateLayout.setVisibility(View.GONE);
        }
    }

    class PracticeBillDateBean implements Serializable {
        private String date;
        private boolean ischecked;
        private boolean isYear;

        PracticeBillDateBean(String date, boolean ischecked, boolean isYear) {
            this.date = date;
            this.ischecked = ischecked;
            this.isYear = isYear;
        }

        boolean isYear() {
            return isYear;
        }

        public void setYear(boolean year) {
            isYear = year;
        }

        public String getDate() {
            return date;
        }

        public void setDate(String date) {
            this.date = date;
        }

        public boolean isIschecked() {
            return ischecked;
        }

        public void setIschecked(boolean ischecked) {
            this.ischecked = ischecked;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        ActionLogUtils.getInstance().onResume(this, ActionConstant.PRACTICE_SCORE_BILL);
    }

    @Override
    protected void onPause() {
        super.onPause();
        ActionLogUtils.getInstance().onPause(this, ActionConstant.PRACTICE_SCORE_BILL);
    }

}
